package com.jxust.vhr.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.jxust.debate.pojo.Cacode;
import com.jxust.debate.pojo.Company;
import com.jxust.debate.pojo.SystemAdministrator;
import com.jxust.debate.util.CommonResult;
import com.jxust.debate.util.ResultEnum;
import com.jxust.vhr.dao.CacodeDao;
import com.jxust.vhr.dao.CompanyDao;
import com.jxust.vhr.dao.SystemAdministratorMapper;
import com.jxust.vhr.service.SysAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SysAdminServiceImpl implements SysAdminService {

    @Autowired
    private SystemAdministratorMapper systemAdministratorDao;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private CacodeDao cacodeDao;

    @Override
    public CommonResult<SystemAdministrator> Login(SystemAdministrator systemAdministrator) {
        CommonResult<SystemAdministrator> commonResult=new CommonResult<>();
        systemAdministrator = systemAdministratorDao.selectByIDAndPassword(systemAdministrator);
        if(systemAdministrator!=null&& !StrUtil.hasEmpty(systemAdministrator.getSaid())){
            commonResult.setResultEnum(ResultEnum.USER_LOGIN_SUCCESS);
            systemAdministrator.setSapassword("");
            commonResult.setData(systemAdministrator);
        }else {
            commonResult.setResultEnum(ResultEnum.USER_LOGIN_FAILED);
        }
        return commonResult;
    }

    /**
     * 为公司创建认证码
     * @param cid 传入公司ID
     * @return 返回创建的认证码（公司管理员认证码首两字母为CA）
     */
    @Override
    public CommonResult<Cacode> CodeCreate(String cid){
        CommonResult<Cacode> commonResult =new CommonResult<>();
        Company company=companyDao.selectById(cid);
        if(company!=null && !StrUtil.hasEmpty(company.getCid())){
            Cacode cacode=new Cacode();
            cacode.setCid(company.getCid());
            cacode.setCacode("CA"+RandomUtil.randomString(30));
            cacodeDao.insert(cacode);
            commonResult.setResultEnum(ResultEnum.SUCCESS);
            commonResult.setData(cacode);
        }else {
            commonResult.setResultEnum(ResultEnum.FAILURE);
        }
        return commonResult;
    }





}
